package com.zbkj.service.service.impl;

import com.zbkj.common.request.PageParamRequest;
import com.github.pagehelper.PageHelper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.common.exception.AppException;

import com.zbkj.common.result.CommonResultCode;
import com.zbkj.common.model.traceable.TraceableOrderDetail;
import com.zbkj.service.dao.TraceableOrderDetailDao;
import com.zbkj.service.service.TraceableOrderDetailService;

import com.zbkj.common.request.TraceableOrderDetailSaveRequest;
import com.zbkj.common.request.TraceableOrderDetailSearchRequest;
import com.zbkj.common.response.TraceableOrderDetailPageDataResponse;
import com.zbkj.common.response.TraceableOrderDetailDetailResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* @author dazongzi
* @description TraceableOrderDetailServiceImpl 接口实现
* @date 2025-07-10
*/
@Service
public class TraceableOrderDetailServiceImpl extends ServiceImpl<TraceableOrderDetailDao, TraceableOrderDetail> implements TraceableOrderDetailService {

    @Resource
    private TraceableOrderDetailDao dao;

    @Override
    public Boolean create(TraceableOrderDetailSaveRequest traceableOrderDetailSaveRequest) {
        TraceableOrderDetail traceableOrderDetail = new TraceableOrderDetail();
        BeanUtils.copyProperties(traceableOrderDetailSaveRequest, traceableOrderDetail, "id");
        boolean save = save(traceableOrderDetail);
        if (!save) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "添加失败");
        }
        return save;
    }

    /**
     * 分页查询溯源订单详情表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<TraceableOrderDetail>
     * @author dazongzi
     * @since 2025-07-10
     */
    @Override
    public List<TraceableOrderDetailPageDataResponse> getList(TraceableOrderDetailSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        //带 TraceableOrderDetail 类的多条件查询
        LambdaQueryWrapper<TraceableOrderDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        // type 类型不能为空 bean中已经限制
        /*
        lambdaQueryWrapper.eq(TraceableOrderDetail::getType, request.getType());

        if (ObjectUtil.isNotEmpty(request.getId()) && request.getId() > 0) {
            lambdaQueryWrapper.eq(TraceableOrderDetail::getId, request.getId());
        }
        if (ObjectUtil.isNotEmpty(request.getName())) {
            lambdaQueryWrapper.like(TraceableOrderDetail::getName, URLUtil.decode(request.getName()));
        }

        if (ObjectUtil.isNotEmpty(request.getStatus())) {
            lambdaQueryWrapper.eq(TraceableOrderDetail::getStatus, request.getStatus());
        }
        if (ObjectUtil.isNotEmpty(request.getMethod())) {
            lambdaQueryWrapper.eq(TraceableOrderDetail::getMethod, request.getMethod());
        }*/

        // 时间区间
        //if (ObjectUtil.isNotEmpty(request.getStarttime()) && ObjectUtil.isNotEmpty(request.getEndtime())) {
        //    lambdaQueryWrapper.between(TraceableOrderDetail::getCreatetime, request.getStarttime(), request.getEndtime());
        //}
        // * -1 已结束 结束时间小于当前时间
        // * 1 进行中 结束时间大于当前时间 && 开始时间小于等于当前时间
        // * 0 未开始 开始时间大于当前时间
        /*
        if (ObjectUtil.isNotEmpty(request.getRunningStatus())) {
            switch (request.getRunningStatus()) {
                case -1:
                    lambdaQueryWrapper.lt(TraceableOrderDetail::getEndtime, currentDate);
                    break;
                case 0:
                    lambdaQueryWrapper.gt(TraceableOrderDetail::getStarttime, currentDate);
                    break;
                case 1:
                    lambdaQueryWrapper.ge(TraceableOrderDetail::getEndtime, currentDate).le(TraceableOrderDetail::getStarttime, currentDate);
                    break;
            }
        }*/

        lambdaQueryWrapper.orderByDesc(TraceableOrderDetail::getCreateTime);
        List<TraceableOrderDetail> traceableOrderDetails = dao.selectList(lambdaQueryWrapper);
        // 添加活动状态
        List<TraceableOrderDetailPageDataResponse> traceableOrderDetailResponses = new ArrayList<>();

        traceableOrderDetails.stream().forEach(x->{
            TraceableOrderDetailPageDataResponse traceableOrderDetailResponse = new TraceableOrderDetailPageDataResponse();
            BeanUtils.copyProperties(x, traceableOrderDetailResponse);
            //traceableOrderDetailResponse.setRunningStatus(x.getStarttime(), x.getEndtime(), currentDate);
            traceableOrderDetailResponses.add(traceableOrderDetailResponse);
        });
        return traceableOrderDetailResponses;
    }

    /**
    * 修改溯源订单详情表
    * @param request
    * @return
    */
    @Override
    public boolean update(TraceableOrderDetailSaveRequest request) {
        if (ObjectUtil.isNull(request.getId()) || request.getId() < 1) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "请传入溯源订单详情表ID");
        }
        getByIdException(request.getId());
        TraceableOrderDetail traceableOrderDetail = new TraceableOrderDetail();
        BeanUtils.copyProperties(request, traceableOrderDetail);
        return updateById(traceableOrderDetail);
    }

    private TraceableOrderDetail getByIdException(Integer id) {
        TraceableOrderDetail traceableOrderDetail = getById(id);
        if (ObjectUtil.isNull(traceableOrderDetail)) {
            throw new AppException("溯源订单详情表不存在");
        }
        if (traceableOrderDetail.getIsDel()) {
            throw new AppException("溯源订单详情表不存在");
        }
        return traceableOrderDetail;
    }

    /**
    * 批量删除
    * @param idList
    * @return
    */
    @Override
    public boolean batchDelete(List<Integer> idList) {
        LambdaUpdateWrapper<TraceableOrderDetail> luw = Wrappers.lambdaUpdate();
        luw.set(TraceableOrderDetail::getIsDel, true);
        luw.in(TraceableOrderDetail::getId, idList);
        boolean update = update(luw);
        if (!update) {
            throw new AppException(CommonResultCode.ERROR.setMessage("批量删除溯源订单详情表失败"));
        }
        return true;
    }

    /**
    * 查询溯源套餐表详情
    * @param id
    * @return
    */
    @Override
    public TraceableOrderDetailDetailResponse getDetails(Integer id) {
        TraceableOrderDetail traceableOrderDetail = getByIdException(id);
        TraceableOrderDetailDetailResponse response = new TraceableOrderDetailDetailResponse();
        BeanUtils.copyProperties(traceableOrderDetail, response);
        return response;
    }

    @Override
    public List<TraceableOrderDetail> getListByOrderId(Integer orderId) {
        LambdaQueryWrapper<TraceableOrderDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(TraceableOrderDetail::getIsDel, false);
        lqw.eq(TraceableOrderDetail::getOrderId, orderId);
        lqw.orderByAsc(TraceableOrderDetail::getCreateTime, TraceableOrderDetail::getId);
        return list(lqw);
    }

}

